package com.zhuss.black.processor;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.black.black.CreateBlackAppendBo;
import com.zhuss.bean.bo.black.black.wechat.CollectBlackBo;
import com.zhuss.bean.vo.black.black.DetailBlackAppendVo;
import com.zhuss.bean.vo.black.black.DetailBlackVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.exception.WpsException;
import com.zhuss.util.JsonUtil;

/**
 * 收藏黑名单 processor
 * @Author: zhushanshuo
 * @Date: 2020/02/28 16:01
 */
@Component("blackCollect")
public class BlackCollect extends AbstractProcessor<Object, CollectBlackBo> implements Processor<Object, CollectBlackBo> {

    @Autowired
	@Qualifier("blackRead")
	private BlackRead blackRead;
    
    @Autowired
	@Qualifier("blackAppendRead")
	private BlackAppendRead blackAppendRead;
    
    @Autowired
	@Qualifier("blackAppendCreate")
	private Processor<Object, CreateBlackAppendBo> blackAppendCreate;
    
    /**
     * 校验
     */
    @Override
    public void check(CollectBlackBo bo) throws WpsException {
    	Long blackId = bo.getBlackId();
    	Integer appendType = bo.getAppendType();
    	
    	DetailBlackAppendVo result = blackAppendRead.getByType(appendType, blackId, bo.getConcurrentUserId());
    	
    	if (result != null) {
    		throw new WpsException(ExceptionCode.黑名单已收藏);
    	}
    }
    
    /**
     * 操作方法
     * @throws WpsException 
     */
    @Override
    public Object process(CollectBlackBo param) throws WpsException {
    	
    	Long blackId = param.getBlackId();
    	DetailBlackVo black = ((DetailBlackVo) blackRead.detail(blackId));
    	String name = black.getTitle();
    	
    	CreateBlackAppendBo bo = new CreateBlackAppendBo();
    	BeanUtils.copyProperties(param, bo);
    	BeanUtils.copyProperties(black, bo);
    	bo.setBlackId(blackId);
    	bo.setType(param.getAppendType());
    	bo.setTitle(black.getTitle());
    	bo.setContent(black.getContent());
    	bo.setPic(black.getPic());
    	blackAppendCreate.process(bo);
    	
    	
    	
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CollectBlackBo param) throws WpsException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.黑名单.key());
    	record.setAction(LogActionEnum.黑名单收藏.key());
    	record.setTargetId(param.getBlackId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
